package com.ts.api.module.channel.service.impl;

import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.channel.ChannelIsRegisterEnum;
import com.ts.api.common.localcache.DistrictCache;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.api.service.ApiUserRegister;
import com.ts.api.module.channel.entity.po.ChannelUserFormFieldPO;
import com.ts.api.module.channel.entity.po.ChannelUserFormPO;
import com.ts.api.module.channel.entity.po.ChannelUserRegisterPO;
import com.ts.api.module.channel.entity.qo.ChannelUserQO;
import com.ts.api.module.channel.service.ChannelUserService;
import com.ts.api.module.channel.service.DoubleWriteUserService;
import com.ts.api.module.channel.service.H5InfoFlowService;
import com.ts.api.module.common.entity.DistrictPO;
import com.ts.cache.form.FormCacheService;
import com.ts.cache.form.entity.FormCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * h5信息流
 *
 * @author caoxin
 */
@Service
public class H5InfoFlowServiceImpl implements H5InfoFlowService {

    @Autowired
    private ChannelUserService channelUserService;
    @Autowired
    private FormCacheService formCacheService;
    @Autowired
    private DoubleWriteUserService doubleWriteUserService;
    @Autowired
    private ApiUserRegister apiUserRegister;

    @Override
    public void save(ApiUserRequest apiUserRequest) {
        Long loanChannelId = apiUserRequest.getLoanChannelId();

        //  获取用户信息
        ChannelUserRegisterPO userRegisterPO = channelUserService.get(ChannelUserQO.createPhone(apiUserRequest.getPhone()));
        //  用户不存在-注册用户及表单信息
        if (ValidParam.isNull(userRegisterPO)) {
            apiUserRegister.applyUserRegisterByApiUser(apiUserRequest);
        } else {
            Long userId = userRegisterPO.getId();

            //  更新用户信息
            ChannelUserRegisterPO updateUserPO = new ChannelUserRegisterPO();
            updateUserPO.setId(userId);
            updateUserPO.setSex(apiUserRequest.getSex());
            updateUserPO.setUserName(apiUserRequest.getUserName());
            updateUserPO.setIdcard(apiUserRequest.getIdcard());
            DistrictPO districtPO = DistrictCache.getByName(apiUserRequest.getCity());
            if (ValidParam.isNotNull(districtPO)) {
                updateUserPO.setAreaCode(districtPO.getCode());
                updateUserPO.setAreaName(districtPO.getName());
            }
            updateUserPO.setIsChannelRegister(ChannelIsRegisterEnum.OLD.getType());
            updateUserPO.setCarLicensePlate(apiUserRequest.getCarLicensePlate());
            doubleWriteUserService.updateUser(updateUserPO);

            //  更新用户资质
            List<FormCache> formCaches = formCacheService.get();
            List<ChannelUserFormFieldPO> listUserFormField = apiUserRequest.toListUserFormField(formCaches);
            List<ChannelUserFormPO> listChannelUserFormPO = CopyUtils.copyList(listUserFormField, ChannelUserFormPO.class);
            IteratorUtils.array(listChannelUserFormPO, t -> {
                t.setChannelId(loanChannelId);
                t.setUserId(userId);
                t.setCreateTime(DateUtils.currentTime());
            });
            doubleWriteUserService.removeByUserId(userId);
            doubleWriteUserService.batchInsertUserForm(listChannelUserFormPO);
        }
    }
}
